package load_balance

import "errors"

/**
轮询负载均衡
*/
type RoundRobinBalance struct {
	curIndex int
	rss      []string
}

func (rb *RoundRobinBalance) Add(params ...string) error {
	if len(params) == 0 {
		return errors.New("params len 1 at least")
	}

	rb.rss = append(rb.rss, params...)
	return nil
}

func (rb *RoundRobinBalance) Next() string {
	if len(rb.rss) == 0 {
		return ""
	}

	if rb.curIndex >= len(rb.rss) {
		rb.curIndex = 0
	}
	defer rb.increase()
	return rb.rss[rb.curIndex]
}

func (rb *RoundRobinBalance) increase() {
	rb.curIndex += 1
}
